#!/bin/sh

#A                                                   B
#                             
#             2001:db8:1::     2001:db8:2::
#         R1---------------R2---------------R3
#
#  
#C                                                    D
#

devs='A C R1 R2 R3 B D'
for dev in $devs
do
	ip netns add $dev
done

#A
ip link add name A_R1 type veth peer name R1_A
ip link set A_R1 netns A
ip netns exec A bash -c \
'
	ip link set lo up
	ip link set A_R1 up

	ip addr add 192.168.0.1/24 dev A_R1
	ip route add 10.0.0.0/24 via 192.168.0.254 dev A_R1
'

#C
ip link add name C_R1 type veth peer name R1_C
ip link set C_R1 netns C
ip netns exec C bash -c \
'
	ip link set lo up
	ip link set C_R1 up

	ip addr add 192.168.0.1/24 dev C_R1
	ip route add 10.0.0.0/24 via 192.168.0.254 dev C_R1
'

#R1
ip link add name R1_R2 type veth peer name R2_R1
ip link set R1_R2 netns R1
ip link set R1_A netns R1
ip link set R1_C netns R1
ip netns exec R1 bash -c \
'
	sysctl net.ipv6.conf.all.forwarding=1
	sysctl net.ipv6.conf.all.seg6_enabled=1
	sysctl net.ipv6.conf.R1_A.seg6_enabled=1
	sysctl net.ipv6.conf.R1_C.seg6_enabled=1
	sysctl net.ipv6.conf.R1_R2.seg6_enabled=1
	sysctl net.vrf.strict_mode=1

	ip link add name vrf_ab type vrf table 100
	ip link set R1_A master vrf_ab
	ip link add name vrf_cd type vrf table 200
	ip link set R1_C master vrf_cd

	links="lo R1_A R1_C R1_R2 vrf_ab vrf_cd"
	for link in $links
	do
		ip link set $link up
	done

	ip addr add 192.168.0.254/24 dev R1_A
	ip addr add 192.168.0.254/24 dev R1_C
	ip addr add 2001:db8:1::12/64 dev R1_R2
	ip addr add fe80::1/64 dev R1_R2
	ip addr add fe80::1/64 dev R1_A
	ip addr add fe80::1/64 dev R1_C

	ip route add 10.0.0.0/24 encap seg6 mode encap segs A2::2,A3::3B dev R1_R2 table 100
	ip route add 10.0.0.0/24 encap seg6 mode encap segs A2::2,A3::3D dev R1_R2 table 200
	ip -6 route add A2::2 via fe80::2 dev R1_R2
'

#R2
ip link add name R2_R3 type veth peer name R3_R2
ip link set R2_R1 netns R2
ip link set R2_R3 netns R2
ip netns exec R2 bash -c \
'
	sysctl net.ipv6.conf.all.forwarding=1
	sysctl net.ipv6.conf.all.seg6_enabled=1
	sysctl net.ipv6.conf.R2_R1.seg6_enabled=1
	sysctl net.ipv6.conf.R2_R3.seg6_enabled=1

	links="lo R2_R1 R2_R3"
	for link in $links
	do
		ip link set $link up
	done

	ip addr add 2001:db8:1::21/64 dev R2_R1
	ip addr add 2001:db8:2::23/64 dev R2_R3
	ip addr add A2::2 dev lo
	ip addr add fe80::2/64 dev R2_R1
	ip addr add fe80::2/64 dev R2_R3

	ip -6 route add A3::3B via fe80::3 dev R2_R3
	ip -6 route add A3::3D via fe80::3 dev R2_R3
'

#R3
ip link add name R3_B type veth peer name B_R3
ip link add name R3_D type veth peer name D_R3
ip link set R3_R2 netns R3
ip link set R3_B netns R3
ip link set R3_D netns R3
ip netns exec R3 bash -c \
'
	sysctl net.ipv6.conf.all.forwarding=1
	sysctl net.ipv6.conf.all.seg6_enabled=1
	sysctl net.ipv6.conf.R3_R2.seg6_enabled=1
	sysctl net.ipv6.conf.R3_B.seg6_enabled=1
	sysctl net.ipv6.conf.R3_D.seg6_enabled=1
	sysctl net.vrf.strict_mode=1

	sysctl net.ipv4.conf.all.rp_filter=0
	sysctl net.ipv4.conf.default.rp_filter=0

	ip link add name vrf_ab type vrf table 100
	ip link set R3_B master vrf_ab
	ip link add name vrf_cd type vrf table 200
	ip link set R3_D master vrf_cd

	links="lo R3_R2 R3_B R3_D vrf_ab vrf_cd"
	for link in $links
	do
		ip link set $link up
	done

	ip addr add 10.0.0.254/24 dev R3_B
	ip addr add 10.0.0.254/24 dev R3_D
	ip addr add 2001:db8:2::32/64 dev R3_R2
	ip addr add fe80::3/64 dev R3_R2
	ip addr add fe80::3/64 dev R3_D
	ip addr add fe80::3/64 dev R3_B


	ip -6 rule add to A3::3B lookup localsid
	ip -6 rule add to A3::3D lookup localsid
	

	ip -6 route add A3::3B encap seg6local action End.DX4 nh4 10.0.0.1 count dev R3_B table 100
	ip -6 route add A3::3D encap seg6local action End.DX4 nh4 10.0.0.1 count dev R3_D table 200

	ip route add 192.168.0.0/24 encap seg6 mode encap segs A2::2,A1::1A dev R3_R2 table 100
	ip -6 route add A2::2 via fe80::2 dev R3_R2 table 100

	ip route add 192.168.0.0/24 encap seg6 mode encap segs A2::2,A1::1C dev R3_R2 table 200
	ip -6 route add A2::2 via fe80::2 dev R3_R2 table 200
'

#B
ip link set B_R3 netns B
ip netns exec B bash -c \
'
	ip link set lo up
	ip link set B_R3 up

	ip addr add 10.0.0.1/24 dev B_R3
	ip route add 192.168.0.0/24 via 10.0.0.254 dev B_R3
'
